package com.coocaa.ops.admin.v1.panel.convert;

import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import com.coocaa.ops.common.core.biz.panel.entity.*;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/16 18:03
 */
@UtilityClass
public class NormalPanelSaveConverter {

    public  static  List<BlockContentInfoEntity> getFinalBlockList(PanelInfoEntity entity){
        List<VirtuallyBlockInfo> blockList = entity.getBlockList();
        return PanelCommonSaveConverter.getFinalBlockList(entity.getStatus(), blockList);
    }


    public static List<DmpRegistryRlsInfoEntity> getFinalDmpList(PanelInfoEntity entity){
        List<DmpRegistryRlsInfoEntity>  result = InstanceUtil.newArrayList();
        List<VirtuallyBlockInfo> blockList = entity.getBlockList();
        int blockSeq = 1;
        for (VirtuallyBlockInfo virtuallyBlockInfo : blockList) {
            handleWrapperDmpRegistryRlsInfoEntity(virtuallyBlockInfo, blockSeq, entity, result);
            blockSeq++;
        }
        return result;
    }

    public static List<PanelBlockRlsEntity> getFinalPanelBlockList(PanelInfoEntity entity){
        List<PanelBlockRlsEntity> result = InstanceUtil.newArrayList();
        List<VirtuallyBlockInfo> blockList = entity.getBlockList();
        int blockSeq = 1;
        for (VirtuallyBlockInfo virtuallyBlockInfo : blockList) {
             handleWrapperPanelBlockRlsEntity(virtuallyBlockInfo, blockSeq, entity, result);
             blockSeq++;
        }
        return result;
    }

    private static void handleWrapperPanelBlockRlsEntity(VirtuallyBlockInfo virtuallyBlockInfo,int blockSeq, PanelInfoEntity entity, List<PanelBlockRlsEntity> result){
        List<BlockContentConfigInfo> commonBlockList = virtuallyBlockInfo.getCommonBlockList();
        int contentSeq = 1;
        for (BlockContentConfigInfo blockContentConfigInfo : commonBlockList){
            PanelBlockRlsEntity panelBlockRlsEntity = getPanelBlockRlsEntity(entity, blockSeq, blockContentConfigInfo, contentSeq);
            result.add(panelBlockRlsEntity);
            contentSeq++;
        }
    }

    private static void handleWrapperDmpRegistryRlsInfoEntity(VirtuallyBlockInfo virtuallyBlockInfo,int blockSeq, PanelInfoEntity entity, List<DmpRegistryRlsInfoEntity>  result){
        List<BlockContentConfigInfo> dmpBlockList = virtuallyBlockInfo.getDmpBlockList();
        int contentSeq = 1;
        if (CollectionUtils.isNotEmpty(dmpBlockList)) {
            for (BlockContentConfigInfo blockContentConfigInfo : dmpBlockList) {
                DmpRegistryRlsInfoEntity dmpRegistryRlsInfoEntity = getDmpRegistryRlsInfoEntity(entity, blockSeq, blockContentConfigInfo, contentSeq);
                if (dmpRegistryRlsInfoEntity != null) {
                    result.add(dmpRegistryRlsInfoEntity);
                    contentSeq++;
                }
            }
        }
    }

    private static DmpRegistryRlsInfoEntity getDmpRegistryRlsInfoEntity(PanelInfoEntity entity, int blockSeq, BlockContentConfigInfo blockContentConfigInfo, int contentSeq){
        Assert.notNull(blockContentConfigInfo.getId(), "推荐位ID不能为空");
        DmpRegistryRlsInfoEntity dmpRegistryRlsInfoEntity = blockContentConfigInfo.getDmpRegistryInfo();

        if (dmpRegistryRlsInfoEntity == null) {
            return null;
        }

        dmpRegistryRlsInfoEntity.setParentContainerId(entity.getId());
        dmpRegistryRlsInfoEntity.setParentContainerVersion(entity.getCurrentVersion());
        dmpRegistryRlsInfoEntity.setParentContainerType(ResourceTypeEnum.PANEL.getValue());
        dmpRegistryRlsInfoEntity.setSubContainerId(blockContentConfigInfo.getId());
        dmpRegistryRlsInfoEntity.setSubContainerType(ResourceTypeEnum.BLOCK.getValue());
        dmpRegistryRlsInfoEntity.setSubContainerSeq(blockSeq);
        dmpRegistryRlsInfoEntity.setSubContainerContentSeq(contentSeq);
        dmpRegistryRlsInfoEntity.setCreator(entity.getModifier());
        dmpRegistryRlsInfoEntity.setModifier(entity.getModifier());
        return dmpRegistryRlsInfoEntity;
    }

    private static PanelBlockRlsEntity getPanelBlockRlsEntity(PanelInfoEntity entity,int blockSeq, BlockContentConfigInfo blockContentConfigInfo, int contentSeq){
        Assert.notNull(blockContentConfigInfo.getId(), "推荐位ID不能为空");
        PanelBlockRlsEntity saveEntity = new PanelBlockRlsEntity();
        saveEntity.setPanelId(entity.getId());
        saveEntity.setPanelVersion(entity.getCurrentVersion());
        saveEntity.setFlagIsSubPanel(0);
        saveEntity.setBlockId(blockContentConfigInfo.getId());
        saveEntity.setBlockSeq(blockSeq);
        saveEntity.setContentSeq(contentSeq);
        saveEntity.setCreator(entity.getModifier());
        saveEntity.setModifier(entity.getModifier());
        saveEntity.setFlag(1);
        return saveEntity;
    }

}
